// pages/haibaofenxiang/haibaofenxiang.js

import {
  ajax,
  src,
  util
} from "../../utils/util.js";

import {
  base64src,
 
} from "../../utils/base64.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    share:"",
    tulist:"",
    current:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this.setData({
      share: options.share
    })


    var that = this

    wx.showLoading({
      title: '加载中...',
    })

    ajax('/generatePoster', {
      data: {
        // userId: this.data.userId,
        share: that.data.share,
        page:'pages/shouxia/shouxia'
      },
      header: {
        "Accept": "application/json"
      },
      method: "post",
    }, res => {
      console.log(res, '获取图片成功')

      wx.hideLoading();

      // 'data:image/png;base64,'
      res.data[0] = 'data:image/png;base64,'+res.data[0]
      res.data[1] = 'data:image/png;base64,' + res.data[1]
      res.data[2] = 'data:image/png;base64,' + res.data[2]


      that.setData({
        tulist:res.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  cb: function (path) {
    // return path
    this.setData({
      path: path
    })
  },

  dianji:function(){

    base64src(this.data.tulist[this.data.current],this.cb)

    wx.previewImage({
      current:this.data.path , 
      urls: [this.data.path] 
    })
  },

 

  huanyizhang:function(){
    if(this.data.current==2){
      this.setData({
        current: 0
      })
    }else{
      this.setData({
        current: this.data.current+1
      })
    }
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  baocun:function(){

    var that=this
    var aa = wx.getFileSystemManager();
    // console.log('that.data.tu:', this.tulist[this.current])
    aa.writeFile({
      filePath: wx.env.USER_DATA_PATH + '/test.png',

      data: that.data.tulist[that.data.current].slice(22),
      
      encoding: 'base64',
      success: res => {
        wx.saveImageToPhotosAlbum({
          filePath: wx.env.USER_DATA_PATH + '/test.png',
          success: function (res) {
            wx.showToast({
              title: '保存成功',
            })
          },
          fail: function (err) {
            console.log(err)
          }
        })
        console.log(res)
      }, fail: err => {
        console.log(err)
      }
    })
 
   
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})